<template>
  <div>
    <el-card>
      <div slot="header">
        密码重置
      </div>
      <el-form
        ref="form"
        :rules="rules"
        :model="resetParams"
        label-width="150px"
        :style="{ width: '90%', margin: '0 auto' }"
      >
        <el-form-item label="请输入旧密码" prop="old_password">
          <el-input
            v-model="resetParams.old_password"
            type="password"
            placeholder="请输入旧密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="请输入新密码" prop="password">
          <el-input
            v-model="resetParams.password"
            type="password"
            placeholder="请输入新密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="请再次输入新密码" prop="password_confirmation">
          <el-input
            v-model="resetParams.password_confirmation"
            type="password"
            placeholder="请再次输入新密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="reset('form')">保 存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import api from "@/api/api.js";
export default {
  name: "Password",
  data() {
    return {
      // 初始数据
      loading: false, //加载组件
      loadingModal: true,
      // 设置参数
      resetParams: {
        old_password: "",
        password: "",
        password_confirmation: ""
      },
      rules: {
        old_password: [
          { required: true, message: "请输入旧密码", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { min: 8, message: "密码至少输入8位", trigger: "blur" },
          { max: 20, message: "密码最多输入20位", trigger: "blur" }
        ],
        password_confirmation: [
          { required: true, message: "请再次输入新密码", trigger: "blur" },
          { min: 8, message: "确认密码至少输入8位", trigger: "blur" },
          { max: 20, message: "确认密码最多输入20位", trigger: "blur" }
        ]
      }
    };
  },
  created() {},
  methods: {
    // 保存设置
    reset(formName) {
      // 数据验证
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          api.admins.password(this.resetParams).then(res => {
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);
              this.init();
            }
          });
        } else {
          this.$message.error(Object.values(error)[0][0].message);
          return false;
        }
      });
    },
    init() {
      this.resetParams = {
        old_password: "",
        password: "",
        password_confirmation: ""
      };
    }
  }
};
</script>

<style lang="less"></style>
